﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HRM.Common;
using System.IO;
namespace HRM.Forms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            //
            dataGridView1.Rows.Add(1, "Do HUy HIep");
            dataGridView1.Rows.Add(2, "ABC");
            dataGridView1.Rows.Add(3, "TUan");

            

            dataGridView1.GotFocus += new EventHandler(dataGridView1_MouseEnter);
            dataGridView1.LostFocus += new EventHandler(dataGridView1_MouseLeave);
        }

        void dataGridView1_MouseLeave(object sender, EventArgs e)
        {
            Button btn = GetExportButton(dataGridView1);
            //if (btn.Visible)
            //{
            //    btn.Visible = false;
            //}
        }

        void dataGridView1_MouseEnter(object sender, EventArgs e)
        {
            Button btn = GetExportButton(dataGridView1);
            btn.Visible =true;
        }

        Button GetExportButton(DataGridView dgv)
        {
            Button btn;
            Control []ctl = dgv.Controls.Find("btnExportExcel", false);
            if (ctl.Length <= 0)
            {
                btn = new Button();
                btn.Name = "btnExportExcel";
                btn.Text = "Xuất Excel";
                btn.Width = 90;
                btn.Height = 30;
                btn.FlatStyle = FlatStyle.Flat;
                btn.FlatAppearance.BorderSize = 1;
                btn.FlatAppearance.BorderColor = Color.Blue;
                

                //btn.Visible = false
                btn.ImageAlign = ContentAlignment.MiddleLeft;
                btn.TextAlign = ContentAlignment.MiddleRight;
                btn.Top = 0;
                btn.Left = dataGridView1.Width - btn.Width;
                btn.Click += new EventHandler(btn_Click);
                dgv.Controls.Add(btn);
                btn.BringToFront();
            }
            else
            {
                btn = ctl[0] as Button;
            }

            return btn;
        }

        void btn_Click(object sender, EventArgs e)
        {
            ExportExcel();
        }

        void ExportExcel()
        {
            string fileName = ExcelHelper.Excel_Export(dataGridView1);
            if (!String.IsNullOrEmpty(fileName))
            {
                try
                {
                    System.Diagnostics.Process.Start(fileName);
                }
                catch (Exception)
                {
                    MessageBox.Show("Không mở được File \n" + Path.GetTempPath() + "\\" + fileName, "Thông báo");
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
    }
}